
#include "GuardDuplicate.h"
#include "Duplicate/DuplicateMgr.h"
#include "Common/TableData/MonsterMonsterCfg.h"
#include "Character/HuoYueDuPart.h"
#include "Character/CreatureMgr.h"
GuardDuplicate::GuardDuplicate()
{
	m_byType = (int8_t)EDuplicateType::EDuplicateType_Guard;
}

GuardDuplicate::~GuardDuplicate()
{

}

//初始化
bool GuardDuplicate::Init(uint32_t nDuplicateID, uint32_t nSceneID, IBrushMonster *pBrush)
{
	bool ret = Duplicate::Init(nDuplicateID, nSceneID, pBrush);
	if (ret)
	{
		//塔怪物ID
		uint32_t towerId = TowerId();
		//订阅塔受到怪物的受击,
		if (towerId > 0)
		{
			SET_UINT64::iterator iterMon = m_setMonster.begin();
			for (; iterMon != m_setMonster.end(); ++iterMon)
			{
				uint64_t mon_cid = (*iterMon);
				Monster *pMonster = m_pScene->GetMonster(mon_cid);
				if (nullptr == pMonster)
				{
					MMOLOG_FMT_ERROR("[logic] GuardDuplicate::Init... GetCreature  failed....duplicateid:%u ,mapid:%u , sceneid:%u , cid:%u, monsid:%u ", m_nDuplicateID, m_nMapID, nSceneID, mon_cid, towerId);
					continue;
				}
				if (towerId == pMonster->GetConfigId())
				{
					m_towerCid = mon_cid;
					LogDebugFmtPrint("[logic] GuardDuplicate::Init...m_towerCid:%lu, towerId:%u,hp:%lu ", m_towerCid,towerId,pMonster->GetAttr(C_HP));
					if (!g_GetEvent()->Subscribe(this, EVENT_UNDER_ATTACK, mon_cid, 0, "Duplicate::Init"))
					{
						MMOLOG_FMT_ERROR("[logic] GuardDuplicate::Init... subcribe monster under attack  event failed....duplicateid:%u ,mapid:%u , sceneid:%u , cid:%u, monsid:%u ", m_nDuplicateID, m_nMapID, nSceneID, mon_cid, towerId);
					}
					break;
				}
			}
		}
		else
		{
			MMOLOG_FMT_ERROR("[logic] GuardDuplicate::Init.....towerid is zero......duplicateid:%u ,mapid:%u , sceneid:%u , towerid:%u ", m_nDuplicateID, m_nMapID, nSceneID, towerId);
		}
	}
	return ret;
}
//
void GuardDuplicate::UnInit()
{
	Duplicate::UnInit();
}

//生物进入场景
void GuardDuplicate::OnCreatureEnter(Creature *pCreature)
{
	if (nullptr == pCreature)
	{
		return;
	}
	if (CREATURE_PLAYER == pCreature->CreatureKind())
	{
		InitExploit(pCreature->GetCid()); //初始化玩家功勋
		//进入副本时移除掉背包中 副本商店物品
		Part *pPackage = pCreature->GetPart(PART_PACKAGE);
		if (nullptr != pPackage)
		{
			SItemSourceParam sourceParam;
			sourceParam.source = S_Drop;
			pPackage->RemoveAllByType((int32_t)EItemSubType::EItemSubType_dup_shop, sourceParam);
		}
		//进入副本，塔防活跃度
		HuoYueDuPart*pHuoyuePart = dynamic_cast<HuoYueDuPart*>(pCreature->GetPart(PART_HUOYUEDU));
		if (nullptr != pHuoyuePart)
		{
			pHuoyuePart->AddHuoYueDuRecord(HYDT_ACTIVITY_GUARD);
		}
		//LogDebugFmtPrint("[logic] GuardDuplicate::EnterDuplicate success...cid:%u ,duplicateid:%u, sceneid:%u ", pCreature->GetCid(), m_nDuplicateID, m_nSceneID);
	}
}


//是否能进入
bool GuardDuplicate::OnCanEnter(Creature *pCreature)
{
	pCreature = pCreature;
	return true;
}

//进入区域
bool GuardDuplicate::OnEnterArea(uint32_t nAreaID)
{
	nAreaID = nAreaID;
	return true;
}
//复活
void GuardDuplicate::OnRelive(Player *pPlayer, int8_t byType)
{
	pPlayer = pPlayer;
	byType = byType;
}

//获取功勋
int32_t GuardDuplicate::GetExploit(CharIDType charId)
{
	return GetCharExploit(charId);
}
//购买功勋商店物品  costExploit : 购买物品需要消耗的功勋
void GuardDuplicate::BuyExploitItem(CharIDType charId, int32_t costExploit)
{
	AddExploit(charId, -costExploit);
}
//主动离开副本之前 的处理 
void GuardDuplicate::OnActiveBeforeLeave(CharIDType cid)
{
	Player *pPlayer = GetPlayer(cid);
	if (nullptr != pPlayer)
	{
		Part *pPackage = pPlayer->GetPart(PART_PACKAGE);
		if (nullptr != pPackage)
		{
			SItemSourceParam sourceParam;
			sourceParam.source = S_Drop;
			pPackage->RemoveAllByType((int32_t)EItemSubType::EItemSubType_dup_shop, sourceParam);
		}
		DelExploit(cid);
	}
	Duplicate::OnActiveBeforeLeave(cid);
	
}
//定时器更新
void GuardDuplicate::OnTimerUpdate(uint32_t nTimerID)
{
	switch (nTimerID)
	{
	case ETimer_Flop:
		{
			//翻牌结束
			OnCommon_FlopStateEnd();
		}
		break;
	case ETimer_Clean:
		{
			OnCommon_CleanStateEnd();
		}
		break;
	case ETimer_Destory:
		{
			LogDebugFmtPrint("[logic] GuardDuplicate destory .... duplicateid:%u , sceneid:%u, mapid:%u ", m_nDuplicateID, m_nSceneID, m_nMapID);
			//回收
			OnCommon_DestoryStateEnd();
		}
		break;
	default:
		break;
	}
}
//执行事件
void GuardDuplicate::OnExecuteEvent(uint16_t nEventID, uint32_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen)
{
	switch (nEventID)
	{
	case EVENT_CREATURE_DEAD:
	{
		if (CREATURE_MONSTER == bySrcType)
		{
			DieEvent *pDieEvent = (DieEvent*)pContext;
			if (pDieEvent->monsterId > 0)
			{
				Player *pPlayer = GetPlayer(pDieEvent->killerCid);
				if (nullptr != pPlayer)
				{
					const MonsterMonsterCfgInfo *pMonCfg = g_GetMonsterMonsterCfgTable()->GetMonsterMonsterCfgInfo(pDieEvent->monsterId);
					if (nullptr != pMonCfg)
					{
						AddExploit(pDieEvent->killerCid, pMonCfg->specialPropertyValue);
					}
				}

			}

		}

	}
	break;
	case EVENT_ENTER_SCENE:
	{
	}
	break;
	case EVENT_DISCONNECT:
	{
	}
	break;
	}
}

void GuardDuplicate::OnReady_State()
{

}

void GuardDuplicate::OnBegin_State()
{
	OnCommon_DupBeginState();
}

void GuardDuplicate::OnFinish_State()
{
	OnCommon_DupFinishState();
}

void GuardDuplicate::OnFlop_State()
{
	//设置抽奖时间 15s
	LogDebugFmtPrint("[logic] GuardDuplicate::OnDuplicateState_Flop....duplicateid:%u , sceneid:%u ", m_nDuplicateID, m_nSceneID);
	OnCommon_DupFlopState();
}

void GuardDuplicate::OnClean_State()
{
	OnCommon_DupCleanState();
}

void GuardDuplicate::OnDestory_State()
{
	OnCommon_DupDestoryState();
}
